//
//  wiimoteCommande.m
//  programme-robot-1
//
//  Created by richard on 12/09/13.
//  Copyright 2013 __MyCompanyName__. All rights reserved.
//

#import "wiimoteCommande.h"
#import  <IOBluetoothUI/objc/IOBluetoothDeviceSelectorController.h>


@implementation wiimoteCommande
@synthesize delegate=delegate,robot=robot;
-(id)init{
	homeButtonState = FALSE;
	leftButtonState = FALSE;
	BButtonState =  FALSE;
	DownButtonState =FALSE ;
	return self;
}
-(BOOL) connect:(NSString *) addresse{
	//wiimoteRef = [[WiiRemote alloc] init];
	NSLog( @"Attempting to connect" );
	
	IOBluetoothDevice *device;

	IOBluetoothDeviceSelectorController *deviceSelector;
	IOBluetoothSDPUUID     *sppServiceUUID;
	NSArray  *deviceArray;
    deviceSelector = [IOBluetoothDeviceSelectorController deviceSelector];
	if ( deviceSelector == nil ) {
		NSLog( @"Error - unable to allocate IOBluetoothDeviceSelectorController.\n" );
		return FALSE;
	}
	[deviceSelector addAllowedUUID:sppServiceUUID];
	if ( [deviceSelector runModal] != kIOBluetoothUISuccess ) {
		NSLog( @"User has cancelled the device selection.\n" );
		return FALSE;
	}       
	deviceArray = [deviceSelector getResults];      
	if ( ( deviceArray == nil ) || ( [deviceArray count] == 0 ) ) {
		NSLog( @"Error - no selected device.  ***This should never happen.***\n" );
		return FALSE;
	}
	
	/**
	 get device from selection
	 **/
	device = [deviceArray objectAtIndex:0];
	//	wiimoteRef = [WiiRemote alloc ];
	NSLog (@" %@ %@",device , wiimoteRef);
	//[wiimoteRef connectTo: device];
	
	return TRUE;
}

-(BOOL) connect{
	discovery = [[WiiRemoteDiscovery alloc] init];
	[discovery setDelegate:self];
	[discovery start];
	
	NSLog (@" %@", [wiimoteRef address]);
	
	return TRUE;
}
- (void) WiiRemoteDiscoveryError:(int)code {
	//[discoverySpinner stopAnimation:self];
	NSLog(@"\n===== WiiRemoteDiscovery error.  If clicking Find Wiimote gives this error, try System Preferences > Bluetooth > Devices, delete Nintendo. (%d) =====", code);
}

- (void) willStartWiimoteConnections {
	NSLog(@"\n===== WiiRemote discovered.  Opening connection. =====");
}

- (void) WiiRemoteDiscovered:(WiiRemote*)wiimote {
	
	//	[discovery stop];
	
	// the wiimote must be retained because the discovery provides us with an autoreleased object
	wiimoteRef = wiimote;
	[wiimote setDelegate:self];
	
	NSLog(@"\n===== Connected to WiiRemote ===== %@", wiimote);
	//[discoverySpinner stopAnimation:self];
	
	[wiimote setLEDEnabled1:YES enabled2:NO enabled3:YES enabled4:NO];
	//[wiimoteQCView setValue:[NSNumber numberWithBool:[led1 state] ] forInputKey:[NSString stringWithString:@"LED_1"]];
	
	[wiimote setMotionSensorEnabled:YES];
	//	[wiimote setIRSensorEnabled:YES];
	NSLog (@" %@", [wiimoteRef address]);

	//[graphView startTimer];
	//[graphView2 startTimer];
	
	//NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
	//[mappingController setSelectionIndex:[[defaults objectForKey:@"selection"] intValue]];
	[self.delegate  afficheAdresse:[wiimoteRef address]];

}


/***
 delgate commande de la wiimote 
***/
#pragma mark WiiRemoteDiscovery delegates
- (void) accelerationChanged:(WiiAccelerationSensorType)type accX:(unsigned short)accX accY:(unsigned short)accY accZ:(unsigned short)accZ{
}
- (void) wiiRemoteDisconnected:(IOBluetoothDevice*)device {
	
	NSLog(@"\n===== lost connection with WiiRemote =====");
}
- (void) buttonChanged:(WiiButtonType)type isPressed:(BOOL)isPressed{
	
	//NSLog(@"\n===== Presse BTN =====");
	if (type == WiiRemoteAButton){
		NSLog(@"\n===== Presse A ===== %@",self);
		if (BButtonState) { 
			BButtonState =FALSE ;
			[delegate sendMoveStop:self.robot ];
		}
		else {
			
			BButtonState = TRUE;
			[delegate sendMoveRacket:self.robot ];
			
		};

	}
	else if (type == WiiRemoteBButton){
		NSLog(@"\n===== Presse B ===== %@",self);
		if (BButtonState) { 
			BButtonState =FALSE ;
			[delegate sendMoveStop:self.robot ];
		}
		else {
			
			BButtonState = TRUE;
			[delegate sendMoveRacket:self.robot ];
			
		};
		
	}
	else if (type == WiiRemoteUpButton){
		NSLog(@"\n===== Presse Left =====%@",self);
		
		if ( homeButtonState) { 
			homeButtonState =FALSE ;
			[delegate sendMoveStop:self.robot ];
		}
		else {

			homeButtonState = TRUE;
			[delegate sendMoveLeft:self.robot ];

		};
		
		
		
	}
	else if (type == WiiRemoteDownButton){
		NSLog(@"\n===== Presse Right =====%@",self);
		if (DownButtonState) { 
			DownButtonState =FALSE ;
			[delegate sendMoveStop:self.robot ];
		}
		else {
			
			DownButtonState = TRUE;
			[delegate sendMoveRight:self.robot ];
			
		};
		
		
	}
	else if (type == WiiRemoteHomeButton){
		NSLog(@"\n===== Presse Home =====%@ %d",self,homeButtonState);
		if (homeButtonState) { homeButtonState =FALSE ;}
		else {
			homeButtonState = TRUE;
		};
			
		if (homeButtonState ==TRUE ) {
			
			[self.delegate connectFromWii:self.robot];
			//self.robot++;
		}
		
	
		
	}
	/*else{
	 NSLog(@"\n=====  release");
	 }
	 */
	
	
}

@end
